package com.yuju.common.core.convert;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.common.util.collection.CollectionUtilss;

@Component
public class BeanConvert<M, P> {
	private static final Logger logger = LoggerFactory.getLogger(AbstractBaseService.class);

	private Class<M> convertClass;
	
	/**
	 * 功能：PO转换VO<br/>
	 * @param from PO对象
	 * @param clazz VO类型对象
	 * @author xuehj
	 * @Date 2017年9月16日 下午12:02:15 <br/>
	 */
	public M convert(P from, Class<M> clazz) {
		if (from == null) {
			return null;
		}
		M to = null;
		try {
			to = clazz.newInstance();
		} catch (Exception e) {
			logger.error("初始化{}对象失败。", clazz, e);
		}
		convert(from, to);
		
		return to;
	}
	
	public List<M> convert(List<P> fromList) {
		if (CollectionUtilss.isEmpty(fromList)) {
            return null;
        }
		this.convertClass = (Class<M>)getSuperClassGenricType(getClass(), 0);
		return convert(fromList, this.convertClass);
	}
	
	/**
     * 批量对象转换
     */
    public List<M> convert(List<P> fromList, Class<M> clazz) {
        if (CollectionUtilss.isEmpty(fromList)) {
            return null;
        }
        List<M> toList = new ArrayList<M>();
        for (P from : fromList) {
            toList.add(convert(from, clazz));
        }
        return toList;
    }
	
	/**
	 * 功能：属性拷贝<br/>
	 * @param from PO对象
	 * @param to Model对象
	 * @author xuehj
	 * @Date 2017年9月16日 下午12:01:02 <br/>
	 */
	protected void convert(P from, M to) {
//		BeanMappingUtil.copy(from, to);
		try {
			BeanUtils.copyProperties(to, from);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 功能：VO转换成PO<br/>
	 * @param from VO对象
	 * @author xuehj
	 * @Date 2017年9月16日 下午12:00:41 <br/>
	 */
	public M convert(P from) {
		this.convertClass = (Class<M>)getSuperClassGenricType(getClass(), 0);
		return convert(from, this.convertClass);
	}
    
    @SuppressWarnings("unchecked")
    private static Class<Object> getSuperClassGenricType(final Class clazz, final int index) {
    	// 返回表示此Class所表示的实体（类、接口、基本类型或void）的直接超类的Type
    	Type genType = clazz.getGenericSuperclass();
    	
    	if (!(genType instanceof ParameterizedType)) {
    		return Object.class;
    	}
    	
    	// 返回表示此类型实际类型参数的Type对象数组
    	Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    	
    	if (index >= params.length || index < 0) {
    		return Object.class;
    	}
    	
    	if (!(params[index] instanceof Class)) {
    		return Object.class;
    	}
    	
    	return (Class) params[index];
    }
}
